package com.tonysong.oa.yeboa12.service.impl;

import com.tonysong.oa.yeboa12.config.util.R;
import com.tonysong.oa.yeboa12.config.util.RedisCache;
import com.tonysong.oa.yeboa12.pojo.Admin;
import com.tonysong.oa.yeboa12.pojo.Menu;
import com.tonysong.oa.yeboa12.mapper.MenuMapper;
import com.tonysong.oa.yeboa12.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tonysong
 * @since 2022-12-01
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RedisCache redisCache;


    @Override
    public R getMenuBysId() {
        System.out.println("menu menu..service");
        Admin admin = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int id=admin.getId();
        System.out.println("服务器获取的登录对象是："+admin);
        //从Redis中获取菜单
        List<Menu> menus= this.redisCache.getCacheList("menu_"+id);//menu_2
        if(menus==null || menus.isEmpty()) {
            menus = this.menuMapper.getMenusById(id);
            //保存到redis中
            this.redisCache.setCacheList("menu"+id,menus);
        }
        return menus !=null ? new R(200, true, menus)
                 : new R(400,false,"未能获取菜单列表！");
    }
}
